package day02.pm;

import javax.swing.*;
import java.awt.*;

/**
 * 下雪🌨
 */
public class Demo01 {
    public static void main(String[] args) {
        Frame frame = new Frame("下雪");
        frame.setSize(1024,768);
        frame.setBackground(Color.BLACK);
        SnowPanel snowPanel = new SnowPanel();
        frame.add(snowPanel);

        frame.show();
//        frame.setVisible(true);
        snowPanel.action();

    }
}
//创建下雪的面板
class SnowPanel extends Panel{
    int[] x = new int[300];
    int[] y = new int[300];
    public SnowPanel(){//这个构造方法在new SnowPanel()时调用
        for(int i=0;i<300;i++){
            x[i] = (int)(Math.random()*1024);
            y[i] = (int)(Math.random()*768);
        }
    }

    public void paint(Graphics g) {
//        g.clearRect(0,0,1024,768);
//        setBackground(Color.BLACK);//有问题
        g.setColor(Color.WHITE);
        for(int i=0;i<300;i++){
            g.drawString("*",x[i],y[i]);
        }


    }

    public void action(){
        while(true){//死循环
            for(int i=0;i<300;i++){
                y[i]++;
                if(y[i]>760){
                    y[i]=0;
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
}
